class_name TeamSelectionTexture
extends Screen

const FLAG_ANCHOR_POINT := Vector2(35,80)
const FLAG_SELECTOR_PREFEB := preload("res://scences/screens/team_selection/flag_selector.tscn")
const NB_COLS := 4
const NB_ROWS := 2

@onready var flags_container: Control = %FlagsContainer

var move_dirs : Dictionary[KeyUtil.Action,Vector2i] = {
	KeyUtil.Action.UP : Vector2i.UP,
	KeyUtil.Action.LEFT : Vector2i.LEFT,
	KeyUtil.Action.RIGHT : Vector2i.RIGHT,
	KeyUtil.Action.DOWN : Vector2i.DOWN
}
var selection : Array[Vector2i] = [Vector2i.ZERO,Vector2i.ZERO]
var selectors: Array[FlagSelector] = []

func _ready() -> void:
	place_flags()
	place_selector()
	
func _process(delta: float) -> void: 
	for i in range(selectors.size()):
		var selector = selectors[i]
		if not selector.is_selected:
			for action: KeyUtil.Action in move_dirs.keys():
				if KeyUtil.is_action_just_pressed(selector.control_scheme,action):
					try_navigation(i, move_dirs[action])
	if not selectors[0].is_selected and KeyUtil.is_action_just_pressed(Player.ControlScheme.P1,KeyUtil.Action.PASS):
		SoundPlayer.play(SoundPlayer.Sound.UI_NAV)
		transition_screen(SoccerGame.ScreenType.MAIN_MENU)
	
func try_navigation(selector_index:int ,direction: Vector2i) -> void:
	var rect :Rect2i = Rect2i(0,0,NB_COLS,NB_ROWS)
	if rect.has_point(selection[selector_index] + direction):
		selection[selector_index] += direction
		var flag_index := selection[selector_index].x + selection[selector_index].y  * NB_COLS
		GameManager.player_setup[selector_index] = DataLoader.get_countries()[1 + flag_index]
		selectors[selector_index].position = flags_container.get_child(flag_index).position
		SoundPlayer.play(SoundPlayer.Sound.UI_NAV)

func place_flags() -> void:
	for j in range(NB_ROWS):
		for i in range(NB_COLS):
			var flag_texture := TextureRect.new()
			flag_texture.position = FLAG_ANCHOR_POINT + Vector2(55* i ,50 * j)
			var country_index = 1+ i + NB_COLS * j
			var country := DataLoader.get_countries()[country_index]
			flag_texture.texture = FlagHelper.get_texture(country)
			flag_texture.scale = Vector2(2,2)
			flag_texture.z_index = 1
			flags_container.add_child(flag_texture)

func place_selector()->void:
	add_selector(Player.ControlScheme.P1)
	if not GameManager.player_setup[1].is_empty():
		add_selector(Player.ControlScheme.P2)
	
func add_selector(control_scheme: Player.ControlScheme) -> void:
	var selector := FLAG_SELECTOR_PREFEB.instantiate()
	selector.position = flags_container.get_child(0).position
	selector.control_scheme = control_scheme
	selector.selected.connect(on_selector_selected.bind())
	selectors.append(selector)
	flags_container.add_child(selector) 
	
func on_selector_selected() -> void:
	for select in selectors:
		if not select.is_selected:
			return
	
	var country_p1 := GameManager.player_setup[0]
	var country_p2 := GameManager.player_setup[1]
	
	if not country_p2.is_empty() and country_p1 != country_p2:
		GameManager.current_match = Match.new(country_p2 , country_p1)
		transition_screen(SoccerGame.ScreenType.IN_GAME)
	else:
		transition_screen(SoccerGame.ScreenType.TOURNAMENT,ScreenData.build().set_tournament(Tournament.new()))
